package com.k.comnote.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.k.comnote.R;
import com.k.comnote.adapter.MyAdapterForNoteTag;
import com.k.comnote.database.NoteDB;
import com.k.comnote.model.ItemNoteTag;
import com.k.comnote.model.NoteTag;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by K on 2015/8/17.
 */
public class Fragment_3 extends Fragment {

    private List<ItemNoteTag> itemNoteTagList;
    private List<NoteTag> mNoteTagList;
    private ListView mNoteTagListView;
    private MyAdapterForNoteTag mNoteTagAdapter;
    private NoteDB noteDB;

    public void update(String tag) {
        ItemNoteTag iTag = new ItemNoteTag(R.drawable.note_tag, tag);
        itemNoteTagList.add(iTag);
        mNoteTagAdapter.notifyDataSetChanged();
       //noteDB = NoteDB.getNoteDB(getActivity());
        NoteTag newTag = new NoteTag();
        newTag.setTag(tag);
        noteDB.saveTag(newTag);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_3, null);
        noteDB = NoteDB.getNoteDB(getActivity());

        mNoteTagList = noteDB.loadNoteTag();
        itemNoteTagList = new ArrayList<>();

        /**
         * 初次初始化标签
         */
        if (mNoteTagList.size() == 0) {
            NoteTag tag1 = new NoteTag();
            tag1.setTag("购物");
            noteDB.saveTag(tag1);
            NoteTag tag2 = new NoteTag();
            tag2.setTag("灵感");
            noteDB.saveTag(tag2);
            NoteTag tag3 = new NoteTag();
            tag3.setTag("重要");
            noteDB.saveTag(tag3);
            mNoteTagList = noteDB.loadNoteTag();
        }
        for (NoteTag note : mNoteTagList) {
            itemNoteTagList.add(new ItemNoteTag(R.drawable.note_tag, note.getTag().toString()));
        }

        mNoteTagListView = (ListView) view.findViewById(R.id.note_tag);
        mNoteTagAdapter = new MyAdapterForNoteTag(getActivity(), itemNoteTagList);
        mNoteTagListView.setAdapter(mNoteTagAdapter);
        mNoteTagListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        return view;
    }
}
